package com.hisan.freeride.home.fragment;

import android.content.Intent;
import android.os.Bundle;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.base.BaseFragment;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.ReserveFragmentBinding;
import com.hisan.freeride.home.activity.InsuranceDetails;
import com.hisan.freeride.home.adapter.InsuranceAdapter;
import com.hisan.freeride.home.model.Insurance;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;


/**
 * 创建时间 : 2017/6/6
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：发布的行程页面
 */
public class InsuranceFragment extends BaseFragment<ReserveFragmentBinding>{
    private InsuranceAdapter adapter;
    private int type=1;
    //页数
    private int pageNumber=1;
    private List<Insurance> fReserves=new ArrayList<>();

    //统一的Fragment构建方法
    public static InsuranceFragment newInstance(int flag) {
        Bundle args = new Bundle();
        //type代表页签，1：进行中 3：成功 2：失败
        args.putInt("type", flag);
        InsuranceFragment fragment = new InsuranceFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.reserve_fragment;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        adapter=new InsuranceAdapter(new ArrayList<>(0));
        mBinding.recy.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        mBinding.recy.setAdapter(adapter);
        Bundle bundle = getArguments();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
            type=bundle.getInt("type");
        }
    }

    @Override
    protected void initEvent() {
        adapter.setButtonClickListener(data -> {
            Bundle bundle=new Bundle();
            bundle.putInt("id",data.getId());
            startActivityForResult(InsuranceDetails.class,99,bundle,true);
        });
        adapter.setOnItemClickListener(data -> {
            //详情
            Bundle bundle=new Bundle();
            bundle.putInt("id",data.getId());
            startActivityForResult(InsuranceDetails.class,99,bundle,true);
        });
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                fReserves.clear();
                pageNumber = 1;
                if (!BaseAppLication.getInstance().ispage())
                    BaseAppLication.getInstance().setIspage(true);
                getData();
                mBinding.pull.finishRefresh();
            }

            @Override
            public void loadMore() {
                pageNumber++;
                if (!BaseAppLication.getInstance().ispage())
                    BaseAppLication.getInstance().setIspage(true);
                getData();
                mBinding.pull.finishLoadMore();
            }
        });
    }

    private void getData(){
        if (BaseAppLication.getInstance().ispage()){
            BaseAppLication.getInstance().setIspage(false);
            OkGo.<List<Insurance>>get(AppConfig.solist)
                    .tag(this)
                    .params("page",pageNumber)
                    .params("size",20)
                    .params("status",type)
                    .execute(new DialogCallback<List<Insurance>>(getActivity()) {
                        @Override
                        public void onSuccess(com.lzy.okgo.model.Response<List<Insurance>> response) {
                            super.onSuccess(response);
                            if (!CollectionUtils.isNullOrEmpty(response.body())){
                                if (isOk(response)){
                                    showData();
                                    if (response.body().size()>=20){
                                        mBinding.pull.setCanLoadMore(true);
                                    }else {
                                        mBinding.pull.setCanLoadMore(false);
                                    }
                                    fReserves.addAll(response.body());
                                    adapter.setNewData(fReserves);
                                }else {
                                    showErrorView(response.code());
                                }
                            }else {
                                if (!CollectionUtils.isNullOrEmpty(fReserves))  adapter.setNewData(fReserves);
                                else noData();
                            }
                        }
                        @Override
                        public void onError(com.lzy.okgo.model.Response<List<Insurance>> response) {
                            super.onError(response);
                            showErrorView(response.code());
                        }
                    });
        }
    }
    @Override
    protected void loadData(boolean isRefresh) {
        fReserves.clear();
        getData();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==99){
            if (!BaseAppLication.getInstance().ispage()){
                BaseAppLication.getInstance().setIspage(true);
            }
            fReserves.clear();
            getData();
        }
    }

}
